package app.v1.common.controller;

import app.v1.common.domain.ao.resource.ResourceCreateAo;
import app.v1.common.domain.vo.resource.ResourceCreateVo;
import app.v1.common.service.ResourceService;
import com.gitee.feizns.quickstart.domain.web.Ret;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 文件上传相关api
 * @author feizns
 * @since 2023/3/13
 */
@Controller
@RequestMapping("/api/v1/resource")
@RequiredArgsConstructor
public class ResourceController {

    private final ResourceService service;

    /**
     * 创建资源（上传文件）
     * @param ao {@link ResourceCreateAo}
     * @return {@link ResourceCreateVo}
     */
    @ResponseBody
    @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Ret<ResourceCreateVo> create(@Validated ResourceCreateAo ao) {
        return Ret.ok(service.create(ao));
    }

    /**
     * 读取文件
     * @param id 资源编号
     */
    @GetMapping("/{id}")
    public String resource(@PathVariable Long id) {
        return "redirect:" + service.getAbUrlById(id);
    }

}
